<?php
class Customer_Model_Ticket extends Zend_Db_Table_Abstract
{
	/**
	 * Model name
	 * @var string
	 */
	protected $_name = 'ticket';
	
	/**
	 * Primary column
	 * @var string
	 */
	protected $_primary = 'ticket_id';
	
	/**
	 * Reference map
	 * @var array
	 */
	protected $_referenceMap = array(
		'Orders' => array(
			'columns' => array('order_id'),
			'refTableClass' => 'Customer_Model_Orders',
			'refColumns' => 'order_id',
			'onDelete' => self::CASCADE,
			'onUpdate' => self::CASCADE
		)
	);
	
	/**
	 * Store array(ticket_id) where fixed_seat = 0
	 * @param array $bookedSeatArray
	 * @return array
	 */
	public function getNonFixedSeatTicketIDArray($bookedSeatArray)
	{
		$ticketIDs = array();
		foreach($bookedSeatArray as $bookedSeat)
		{
			if($bookedSeat['fixed_seat'] == 0)
				$ticketIDs[] = $bookedSeat['ticket_id'];
		}
		return $ticketIDs;
	}

	/**
	 * Store array(seat_number => fixed_seat, ...)
	 * @param array $bookedSeatArray
	 * @return array
	 */
	public function getSeatNumberStatusMap($bookedSeatArray)
	{
		$map = array();
		foreach($bookedSeatArray as $bookedSeat)
		{
			$map[$bookedSeat['seat_number']] = $bookedSeat['fixed_seat'];
		}
		return $map;
	}

	/**
	 * Check if this seat is free
	 * @param int $seatNumber
	 * @param int $tripID
	 * @return int
	 */
	private function _isThisSeatFree($seatNumber, $tripID)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$stmt = $db->query("SELECT is_this_seat_free(?, ?)", array($seatNumber, $tripID));
		$result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
		return empty($result) ? null : (int)$result[0][0];
	}

	/**
	 * Create ticket
	 * @param int $orderID
	 * @param int $seatNumber
	 * @param bool $fixedSeat
	 * @param int $tripID
	 * @return int
	 */
	public function createTicket($orderID, $seatNumber, $fixedSeat = 1, $tripID)
	{
		if($this->_isThisSeatFree($seatNumber, $tripID)) {
			$ticketRow = $this->createRow();
			$ticketRow->order_id = $orderID;
			$ticketRow->seat_number = $seatNumber;
			$ticketRow->fixed_seat = $fixedSeat;
			return $ticketRow->save();
		}
		throw new Zend_Exception('Could not create ticket. The seat was booked.');
	}
	
	/**
	 * Update temporary seat
	 * @param int $ticketID
	 * @param int $seatNumber
	 * @return int
	 */
	public function updateTempSeat($ticketID, $seatNumber)
	{
		$ticketRow = $this->find($ticketID)->current();
		$ticketRow->seat_number = $seatNumber;
		return $ticketRow->save();
	}
	
	/**
	 * Get booked seat array
	 * @param unknown_type $tripID
	 * @return array
	 */
	public function getBookedSeatArray($tripID)
	{		
		$db = Zend_Db_Table::getDefaultAdapter(); 
		$stmt = $db->query("CALL list_booked_seat(?)", array($tripID));
		$bookedSeatArray = $stmt->fetchAll();
		return $bookedSeatArray;
	}
	
	/**
	 * Get total seat count
	 * @param int $tripID
	 * @return int
	 */
	public function getTotalSeatCount($tripID)
	{
		$db = Zend_Db_Table::getDefaultAdapter(); 
		$stmt = $db->query("SELECT get_seat_count_by_trip(?)", array($tripID));
		$result = $stmt->fetchAll(Zend_Db::FETCH_NUM);

		return empty($result) ? null : $result[0][0];
	}
}